package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 *
 * @author lo
 * @RestControllerAdvice Springmvc 异常处理拦截注解
 */
@RestControllerAdvice
@Slf4j
@Configuration
public class ExceptionCatch {

    /**
     * 记录项目中出现的所有异常
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult exception(Exception ex){
        ex.printStackTrace();
        //记录日志
        log.error("ExceptionCatch ex:{}", ex);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "您的网络异常，请稍后重试");
    }

    /**
     * 拦截自定义异常
     * @param ex
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public ResponseResult customException(CustomException ex){
        ex.printStackTrace();
        //记录日志
        log.error("ExceptionCatch ex:{}", ex);
        //获取异常信息
        AppHttpCodeEnum appHttpCodeEnum = ex.getAppHttpCodeEnum();
        //返回异常信息
        return ResponseResult.errorResult(appHttpCodeEnum);
    }
}
